<?php
// contact to database
$connect = mysql_connect("localhost", "root", "") or die ("Error , check your server connection.");
mysql_select_db("stms");
 
//Get data in local variable
$Regno=$_POST['Regno'];
$Indexno=$_POST['Indexno'];
$Year=$_POST['Year'];
$School=$_POST['School'];
$District=$_POST['District'];
$Stream=$_POST['Stream'];
$Subject=$_POST['Subject'];
$Result=$_POST['Result'];
$Zscore	=$_POST['Zscore'];
$DistrictRank=$_POST['DistrictRank'];
$IslandRank=$_POST['IslandRank'];



$result=mysql_query("SELECT * FROM gcealdetails WHERE Regno='$Regno'");
 $insert="Add";
 $update="Update";
 $delete="Delete";
 
 if($_POST['ADD']==$insert)
 {
 
 if (mysql_num_rows($result) == 1){
  	 header("Location:http://localhost/stms/pages/aladmin.php?err=This Regno is already exist!");

 //echo "This Regno is already exist";
 }
 else
 {
// check for null values
	if ($Regno==""){
	  	 header("Location:http://localhost/stms/pages/aladmin.php?err=Regno must be entered, hit back button and re-enter information");

	//echo "All fields must be entered, hit back button and re-enter information";
	}
	else{
	$query="insert into gcealdetails(Regno,Indexno,Year,School,District,Stream,Result,Zscore,DistrictRank,IslandRank) values('$Regno','$Indexno','$Year','$School','$District','$Stream','$Result','$Zscore	','$DistrictRank','$IslandRank')";
	mysql_query($query)  or die(mysql_error());
	 	header("Location:http://localhost/stms/pages/aladmin.php?err=Your message has been received!");

	//echo "Your message has been received";
	}
	}
}
else if($_POST['UPDATE']==$update)
{
if (mysql_num_rows($result) == 1)
{
	$upd="UPDATE gcealdetails SET Indexno='$Indexno',Year='$Year',School='$School',District='$District',Stream='$Stream',Result='$Result',Zscore='$Zscore'	,DistrictRank='$DistrictRank',IslandRank='$IslandRank' WHERE Regno='$Regno'";

	mysql_query($upd)  or die(mysql_error());
	 	 header("Location:http://localhost/stms/pages/aladmin.php?err=Updated successfully!");

	//echo "Updated successfully";
 }
 
 else 
 	 	 header("Location:http://localhost/stms/pages/aladmin.php?err=There is no Regno as you given!");

 //echo "There is no Regno as you given";
 }
 else if($_POST['DELETE']==$delete)
 {
 
if (mysql_num_rows($result) == 1)
 {
 $del="DELETE FROM gcealdetails WHERE Regno = '$Regno'";
 mysql_query($del)  or die(mysql_error());
  	 header("Location:http://localhost/stms/pages/aladmin.php?err=Deleted successfully!");

	//echo "Deleted successfully";
	}
	else
	 	 header("Location:http://localhost/stms/pages/aladmin.php?err=Regno is not there!");

	//echo "Regno is not there";
	
 
 }
 if(isset($_POST['Search']))
 {
	header("Location:http://localhost/stms/pages/aladmin.php?err1=$Regno");
 }
 if(isset($_POST['Search2']))
 {
  $se=$_POST['Searcher'];
	header("Location:http://localhost/stms/pages/aladmin.php?err2=$se");
 }
?>